home *** CD-ROM | disk | FTP | other *** search
- Path: thales.nmia.com!not-for-mail
- From: ghealton@nmia.com (Gilbert Healton)
- Newsgroups: comp.lang.c++
- Subject: Re: Output to file vs. cout
- Date: 19 Mar 1996 21:02:29 GMT
- Organization: New Mexico Internet Access
- Message-ID: <4in7d5$9el@thales.nmia.com>
- References: <JL.96Mar7165152@thyme.id.dth.dk> <4i18mg$ba5@hpbblb.bbn.hp.com> <4impgh$jnl@is-news.gov.ab.ca>
- NNTP-Posting-Host: socrates.nmia.com
- X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]
-
- Ron Orr (orr@lisd.env.gov.ab.ca) wrote:
- : Matthias Dittrich (matti) wrote:
- : : jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
- : : >
- : : >Here's another of the probs that comes from porting C to C++:
- : : >
- : : >- I want to be able to redirect program output to either a file or cout.
- : : > Typically this would be selected by a commandline option ("-o outfile").
- : : >
- : : >- In C I would do something like this:
- : : >
- : : >
- : : > main()
- : : > {
- : : > FILE *outputfile;
- : : >
- : : > if (commandline == do_output_to_file)
- : : > outputfile = stdout;
- : : > else
- : : > outputfile = fopen(passed_filename, "w");
- : : >
- : : > fprintf(outfile, "Hello world\n");
- : : >
- : : > fclose(outfile); /* Maybe - not allways necassary */
- : : > }
- : : >
- : : >- The question is: How is this done in C++ ?
- : : >
- : : >.......
- : : ofstream outfile(passed_filename); /* this constructor opens your file */
- : : outfile << "Hello world" << endl; /* writes the string */
- :
- : : The destructor closes the file and of course you should do some error checking.
- :
-
- I, too, am looking for a solution to this. It seems it is taking me longer
- to get all the ins and outs of streams than the C++ language..
-
- The ability to have a stream that you open, and close/reopen, at various points
- of the file is also of interest.
-
- --
- ----- Computer Consulting / Web Pages ----- http://www.nmia.com/~ghealton/
- These opinions are my own. Life is learning and I may retract, modify,
- even attack, my previous ideas at any time without notice.
-